//
//  StatusPictureView.swift
//  Weibo T
//
//  Created by mac on 15/12/1.
//  Copyright © 2015年 陈肖坤. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

private let pictureCellID = "pictureCellID"
private let pictureCellMargin: CGFloat = 5.0001

class StatusPictureView: UICollectionView {
    
    
    //配图视图 设置模型数据
    var imageURLs: [NSURL]? {
        didSet {
            testLabel.text = "\(imageURLs?.count ?? 0)"
            let pSize = caclePictureViewSize()
            self.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(pSize)
            }
            //属性试图
            reloadData()
        }
    }
    /*
    单图会按照图片等比例显示
    多图的图片大小固定
    多图如果是4张，会按照 2 * 2 显示
    多图其他数量，按照 3 * 3 九宫格显示
    */
    
    private func caclePictureViewSize() -> CGSize {
        
        let imageCount = imageURLs?.count ?? 0
        let maxWidth = screenW - 2 * StatusCellMargin
        let itemWidth = (maxWidth - 2 * pictureCellMargin) / 3
        
        //设置CollectionView itemSize
        //设置cell的大小
        let layout = collectionViewLayout as? UICollectionViewFlowLayout
        layout?.itemSize = CGSizeMake(itemWidth, itemWidth)
        
        if imageCount == 0 {
            return CGSizeZero
        }
        
        if imageCount == 1 {
            //TODO: 先显示固定大小
            let imageSize = CGSizeMake(180, 120)
            return imageSize
        }
        
        if imageCount == 4 {
            let pW = itemWidth * 2 + pictureCellMargin
            return CGSizeMake(pW, pW)
        }
        
        //程序走到这里 就是其他多张情况
        //多图其他数量，按照 3 * 3 九宫格显示
        
        //先除 ->  +1  -> 分子 -1
       
        let row = CGFloat((imageCount - 1) / 3 + 1)
        
        return  CGSizeMake(maxWidth, itemWidth * row + pictureCellMargin * (row - 1))
    }
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        let layout = UICollectionViewFlowLayout()
        layout.minimumInteritemSpacing = pictureCellMargin
        layout.minimumLineSpacing = pictureCellMargin
        super.init(frame: CGRectZero, collectionViewLayout: layout)
        //注册cell
        registerClass(picteureCell.self, forCellWithReuseIdentifier: pictureCellID)
        
        //设置数据源
        dataSource = self
        backgroundColor = UIColor.whiteColor()
        
        //添加测试label
        setupUI()
        
    }

    private func setupUI() {
        addSubview(testLabel)
        testLabel.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.center)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //懒加载测试label
    private lazy var testLabel: UILabel = UILabel(title: "", color: UIColor.redColor(), fontSize: 30, margin: 0)
    
}
//MARK: 数据源方法
extension StatusPictureView: UICollectionViewDataSource {

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageURLs?.count ?? 0
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell  = collectionView.dequeueReusableCellWithReuseIdentifier(pictureCellID, forIndexPath: indexPath) as! picteureCell
        cell.imageURL = imageURLs![indexPath.item]
       
        return cell
    }

}

class picteureCell: UICollectionViewCell {
    //定义外部设置属性
    var imageURL: NSURL? {
        didSet {
            iconImageView.sd_setImageWithURL(imageURL)
        }
    }

    //MRAK: 重写构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //MRAK: 设置UI
    private func setupUI() {
    
        contentView.addSubview(iconImageView)
        
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView.snp_edges)
        }
    }
    //MRAK:懒加载子控件
    //图片默认显示  scaletofill
    private lazy var iconImageView: UIImageView = {
       let iv = UIImageView()
       iv.clipsToBounds = true
        iv.contentMode = .ScaleAspectFill
        return iv
    }()
    
}

                                                                                       